var HOST_ADDRESS = 'http://166.111.112.88:8080/qingxinchuanmei/';
var HOST_IP = 'http://166.111.112.88:8080';
var PUBLICATE_NEWS = 0;
var NEWS_ADD = 1;
var NEWS_EDIT = 2;
var NEWS_DELETE = 3;
var GET_NEWS_ABSTRACT = 4;
var GET_NEWS_DETAIL = 5;
var GET_NEWS_ALLNUMBER = 6;
var INFO_ADD = 7;
var GET_NEWS_LATEST = 8;
var GET_JOURNALIST_LIST = 9;
var JOURNALIST_ADD = 10;
var JOURNALIST_EDIT = 11;
var JOURNALIST_DELETE = 12;
var ALBUM_GETALL = 13;
var ALBUM_GET = 14;
var ALBUM_ADD = 15;
var ALBUM_DELETE = 16;
var ALBUM_EDIT = 17;
var FEEDBACK_GETALL = 18;
var INFO_GETALL = 19;
var INFO_DELETE = 20;
var COMMENT_GETALL = 21;
var COMMENT_DELETE = 22;
var NOTIFICATION = 23;

var current_api_code;
var journalists;   //记者列表

var API_URI = [
	'admin/news/publish',	//0,发布一期新闻
	'admin/news/add',		//1,添加一条新闻
	'admin/news/edit',			//2,编辑一条新闻
	'admin/news/delete',		//3,删除一条新闻
	'admin/news/abstract',		//4,获取一期新闻的摘要
	'admin/news/getDetail',		//5,获取一条新闻的详细信息
	'admin/news/getAllNumbers',	//6,获取所有的期号
	'admin/information/add',			//7.添加一个资讯
	'admin/news/getAbstract', //8. 获得最新几期
	'admin/journalist/getAll', //9. 获得记者列表
	'admin/journalist/add',  //10. 新增加一个记者
	'admin/journalist/edit', //11. 编辑一个记者
	'admin/journalist/delete', //12. 删除一个记者
	'admin/album/getAll',    //13. 获得所有相册
	'admin/album/get',    //14. 获得某个相册
	'admin/album/add',    //15. 添加某个相册
	'admin/album/delete',    //16. 删除某个相册
	'admin/album/edit',    //17. 编辑某个相册	
	'admin/feedback/getAll', //18. 获得所有的反馈
	'admin/information/getAll',			//19. 获得所有的咨询
	'admin/information/delete',	//20. 删除资讯
	'admin/comment/getAll',  //21. 获得所有评论
	'admin/comment/delete',	//22. 删除评论
	'admin/notification',		//23. 远程通知
];

//发布新闻
function publicateVolumn(number, ifPublish){
	if (DEBUG) console.log('Call publicate number = ' + number);
	var data = {};
	data.number = number;
	data.published = ifPublish;
	APIClient('POST', PUBLICATE_NEWS, null, data);
}

//获得所有期号
function getAllNumbers()
{
	if (DEBUG) console.log('Call getAllNumbers');
	//var params = {};
	APIClient('GET', GET_NEWS_ALLNUMBER, null, null);
}

//获得最新几期概述
function getLatest()
{
		if (DEBUG) console.log('Call getLatest bug');
		APIClient('GET', GET_NEWS_LATEST, null, null);
}

//获得某新闻的详细描述
function getNewsDetail(nid) {
	if (DEBUG) console.log('Call getNewsDetail, nid = ' + nid);
	var params = {};
	params.nid = nid;
	APIClient('GET', GET_NEWS_DETAIL, params, null);
}

//新增新闻
function newsAdd()
{
	if (DEBUG) console.log('Call newsAdd bug');
	var detail = $('#news-create');
	var data = {};
	var news = {};
	news.title = detail.find('input[name="title"]').val();
	news.content = detail.find('textarea[name="content"]').val();
	news.date = detail.find('input[name="date"]').val();
	news.cwb = detail.find('input[name="cwb"]').val();
	news.number = '1';
	news.url = detail.find('input[name="url"]').val();
	var type = detail.find('select[name="type"]').val();
	if (type >= 4)
	{
		news.type = 0;
		news.isNews = "false";
	}
	else
	{
		news.type = type;
		news.isNews = "true";
	}
	news.cover = detail.find('input[name="cover"]').val();
	var journalists = $.trim(detail.find('input[name="journalists"]').val()).split(' ');
	var picNum = detail.find('input[name="picNum"]').val();
	var photos = [];
	var descriptions = [];
	for (i = 0; i < picNum; i++)
	{
		//var idString  = 
		photos[i] = detail.find('input[name="pic' + i + 'id"]').val();
		descriptions[i] = detail.find('input[name="pic' + i + 'description"]').val();
	}
	news.journalists = journalists;
	news.photos = photos;
	news.descriptions = descriptions;
	//检查是否有投票
	if (detail.find('input[name="hasVote"]').attr("checked"))
	{
		var vote = {};
		vote.title = detail.find('input[name="title"]').val();
		vote.question = detail.find('input[name="question"]').val();
		vote.end = detail.find('input[name="endDate"]').val();
		vote.start = "2010-05-09";
		var optionNum = detail.find('input[name="optionNum"]').val();
		var options = [];
		for (i = 0; i < optionNum; i++)
		{
			options[i] = detail.find('input[name="option' + i + '"]').val();
		}
		vote.options = options;
		news.vote = vote;
	}
	news.tags = [];
	
	data.news = news;
	jsondata = JSON.stringify(news);
	
	APIClient('POST', NEWS_ADD, null, {news:jsondata});
}

//编辑新闻
function newsEdit(id)
{
	if (DEBUG) console.log('Call newsEdit bug');
	var detail = $('#news-detail');
	var data = {};
	var news = {};
	news.id = id;
	news.title = detail.find('input[name="title"]').val();
	news.content = detail.find('textarea[name="content"]').val();
	news.date = detail.find('input[name="date"]').val();
	news.cwb = detail.find('input[name="cwb"]').val();
	news.url = detail.find('input[name="url"]').val();
	news.number = '1';
	var type = detail.find('select[name="type"]').val();
	if (type >= 4)
	{
		news.type = 0;
		news.isNews = "false";
	}
	else
	{
		news.type = type;
		news.isNews = "true";
	}
	news.cover = detail.find('input[name="cover"]').val();
	var journalists = $.trim(detail.find('input[name="journalists"]').val()).split(' ');
	var picNum = detail.find('input[name="picNum"]').val();
	var photos = [];
	var descriptions = [];
	for (i = 0; i < picNum; i++)
	{
		photos[i] = detail.find('input[name="pic' + i + 'id"]').val();
		descriptions[i] = detail.find('input[name="pic' + i + 'description"]').val();
	}
	news.journalists = journalists;
	news.photos = photos;
	news.descriptions = descriptions;
	//检查是否有投票
	if ((detail.find('input[name="hasVote"]').attr("checked")) && (detail.find('input[name="modifyVote"]').attr("checked")))
	{
		var vote = {};
		vote.title = detail.find('input[name="title"]').val();
		vote.question = detail.find('input[name="question"]').val();
		vote.end = detail.find('input[name="endDate"]').val();
		vote.start = "2010-05-09";
		var optionNum = detail.find('input[name="optionNum"]').val();
		var options = [];
		for (i = 0; i < optionNum; i++)
		{
			options[i] = detail.find('input[name="option' + i + '"]').val();
		}
		vote.options = options;
		news.vote = vote;
	}
	news.tags = [];
	
	data.news = news;
	jsondata = JSON.stringify(news);
	
	APIClient('POST', NEWS_EDIT, null, {news:jsondata});
}

//获得所有相册
function albumGetall()
{
	if (DEBUG) console.log('Call getJournalist bug');
	APIClient('GET', ALBUM_GETALL, null, null);
}

//获得某相册
function albumGet(id)
{
	if (DEBUG) console.log('Call getJournalist bug');
	APIClient('GET', ALBUM_GET, null, {aid:id});
}

//添加相册
function albumAdd()
{
	if (DEBUG) console.log('Call albumAdd bug');
	var detail = $('#album-create');
	var data = {};
	var album = {};
	var photos = [];
	var descriptions = [];
	var photographers = [];
	
	album.name = detail.find('input[name="name"]').val();
	album.introduction = detail.find('input[name="introduction"]').val();
	var picNum = detail.find('input[name="picNum"]').val();
	album.cover = detail.find('input[name="cover_id"]').val();
	
	for (i = 0; i < picNum; i++)
	{
		photos[i] = detail.find('input[name="pic'+ i +'id"]').val();
		descriptions[i] = detail.find('input[name="pic'+ i +'description"]').val();
		photographers[i] = detail.find('input[name="pic'+ i +'photographer"]').val();
	}
	album.photos = photos;
	album.descriptions = descriptions;
	album.photographers = photographers;
	
	jsondata = JSON.stringify(album);
	
	APIClient('POST', ALBUM_ADD, null, {album:jsondata});
}

//删除相册
function albumDelete(id)
{
	if (DEBUG) console.log('Call albumDelete bug');
	APIClient('POST', ALBUM_DELETE, null, {aid:id});
}

//编辑相册
function albumEdit(id)
{
	if (DEBUG) console.log('Call albumEdit bug');
	var detail = $('#album-detail');
	var data = {};
	var album = {};
	var photos = [];
	var descriptions = [];
	var photographers = [];
	
	album.name = detail.find('input[name="name"]').val();
	album.introduction = detail.find('input[name="introduction"]').val();
	var picNum = detail.find('input[name="picNum"]').val();
	album.cover = detail.find('input[name="cover_id"]').val();
	
	for (i = 0; i < picNum; i++)
	{
		photos[i] = detail.find('input[name="pic'+ i +'id"]').val();
		descriptions[i] = detail.find('input[name="pic'+ i +'description"]').val();
		photographers[i] = detail.find('input[name="pic'+ i +'photographer"]').val();
	}
	album.photos = photos;
	album.descriptions = descriptions;
	album.photographers = photographers;
	
	jsondata = JSON.stringify(album);
	
	APIClient('POST', ALBUM_EDIT, null, {album:jsondata, aid:id});
}
//删除记者                                                                                             
function newsDelete(id){
	if (DEBUG) console.log('Call newsDelete bug');
	//var params = {};
	//params.jid = id;            
	APIClient('POST', NEWS_DELETE, null, {nid:id});
}   

//获得记者列表
function getJournalists(){
		if (DEBUG) console.log('Call getJournalist bug');
		APIClient('GET', GET_JOURNALIST_LIST, null, null);
}

//上传记者
function journalistAdd(){
	if (DEBUG) console.log('Call journalistAdd bug');
	var detail = $('#jour-create');
	var data = {};
	var journalist = {};
	journalist.avatar = detail.find('input[name="avatar_id"]').val();
	journalist.birthday = detail.find('input[name="birthday"]').val();
	journalist.class = detail.find('input[name="class"]').val();
	journalist.constellation = detail.find('input[name="constellation"]').val();
	journalist.introduction = detail.find('textarea[name="introduction"]').val();
	journalist.motto = detail.find('input[name="motto"]').val();
	journalist.name = detail.find('input[name="name"]').val();
	journalist.sex = detail.find('input[name="sex"]').val();
	if (detail.find('input[name="isRetired"]').attr("checked"))
		journalist.isRetired = "true";
	else
		journalist.isRetired = "false";
	//journalist.isRetired = detail.find('input[name="isRetired"]').attr("checked");
	
	data.journalist = journalist;
	jsondata = JSON.stringify(journalist);
	
	APIClient('POST', JOURNALIST_ADD, null, {journalist:jsondata});
}                                   

//编辑记者                                                                                             
function journalistEdit(id){                                                                          
	if (DEBUG) console.log('Call journalistAdd bug');                                                
	var detail = $('#journalist-detail');                                                                  
	var data = {};                                                                                   
	var journalist = {};
	journalist.jid = id;                                                          
	journalist.avatar = detail.find('input[name="avatar_id"]').val();                                
	journalist.birthday = detail.find('input[name="birthday"]').val();                               
	journalist.class = detail.find('input[name="class"]').val();                                     
	journalist.constellation = detail.find('input[name="constellation"]').val();                     
	journalist.introduction = detail.find('textarea[name="introduction"]').val();                    
	journalist.motto = detail.find('input[name="motto"]').val();                                     
	journalist.name = detail.find('input[name="name"]').val();                                       
	journalist.sex = detail.find('input[name="sex"]').val();     
	if (detail.find('input[name="isRetired"]').attr("checked"))
		journalist.isRetired = "true";
	else
		journalist.isRetired = "false";                             
	                                                                                                 
	data.journalist = journalist;                                                                    
	jsondata = JSON.stringify(journalist);                                                           
	                                                                                                 
	APIClient('POST', JOURNALIST_EDIT, null, {journalist:jsondata});                                  
}

//删除记者                                                                                             
function journalistDelete(id){
	if (DEBUG) console.log('Call journalistDelete bug');
	//var params = {};
	//params.jid = id;            
	APIClient('POST', JOURNALIST_DELETE, null, {jid:id});
}            

//获得所有反馈
function feedbackGetall()
{
	if (DEBUG) console.log('Call feedback getall bug');
	APIClient('GET', FEEDBACK_GETALL, null, null);
}             

//获得所有资讯
function infoGetall()
{
	if (DEBUG) console.log('Call INFORMATION getall bug');
	APIClient('GET', INFO_GETALL, null, null);
}

//增加一个咨询
function infoAdd()
{
	if (DEBUG) console.log('Call journalistAdd bug');
	var detail = $('#info-create');
	var data = {};
	var information = {};
	information.title = detail.find('input[name="title"]').val();
	information.content = detail.find('textarea[name="content"]').val();
	information.pid = detail.find('input[name="pid"]').val();
	information.start = detail.find('input[name="start"]').val();
	information.end = detail.find('input[name="end"]').val();
	
	jsondata = JSON.stringify(information);
	
	APIClient('POST', INFO_ADD, null, {information:jsondata});
}

//删除一个咨询
function infoDelete(id)
{
	if (DEBUG) console.log('Call journalistDelete bug');
	//var params = {};
	//params.jid = id;            
	APIClient('POST', INFO_DELETE, null, {iid:id});
}               

//获得所有评论
function commentGetall()
{
	if (DEBUG) console.log('Call INFORMATION getall bug');
	APIClient('GET', COMMENT_GETALL, null, null);
} 

//删除一条评论
function commentDelete(id)
{
	if (DEBUG) console.log('Call journalistDelete bug');            
	APIClient('POST', COMMENT_DELETE, null, {cid:id});
}                                        

//远程通知
function notification()
{
	if (DEBUG) console.log('Call journalistDelete bug');
	var notification = $('#notification').find('input[name="notification"]').val();
	APIClient('POST', NOTIFICATION, null, {notification:notification});
}          